package com.photoeditor.demo.ui.widget;

import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.common.base.utils.thread.ThreadPool;
import com.photoeditor.R;

/**
 * Created by mutour on 2017/10/27.
 */

public class RatingBar extends LinearLayout implements View.OnClickListener {

    protected ImageView mStarOne;
    protected ImageView mStarTwo;
    protected ImageView mStarThree;
    protected ImageView mStarFour;
    protected ImageView mStarFive;
    private OnRatingClickListener mOnRatingClickListener;


    public RatingBar(Context context) {
        this(context, null);
    }

    public RatingBar(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public RatingBar(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public RatingBar(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();


        mStarOne = (ImageView) findViewById(R.id.start_one);
        mStarTwo = (ImageView) findViewById(R.id.start_two);
        mStarThree = (ImageView) findViewById(R.id.start_three);
        mStarFour = (ImageView) findViewById(R.id.start_four);
        mStarFive = (ImageView) findViewById(R.id.start_five);
        mStarOne.setVisibility(View.INVISIBLE);
        mStarTwo.setVisibility(View.INVISIBLE);
        mStarThree.setVisibility(View.INVISIBLE);
        mStarFour.setVisibility(View.INVISIBLE);
        mStarFive.setVisibility(View.INVISIBLE);
        mStarOne.setOnClickListener(this);
        mStarTwo.setOnClickListener(this);
        mStarThree.setOnClickListener(this);
        mStarFour.setOnClickListener(this);
        mStarFive.setOnClickListener(this);

        ThreadPool.runUITask(new Runnable() {
            @Override
            public void run() {
                startStarAnimation(mStarOne, 0);
                startStarAnimation(mStarTwo, 80);
                startStarAnimation(mStarThree, 160);
                startStarAnimation(mStarFour, 240);
                startStarAnimation(mStarFive, 320);
            }
        }, 400);
    }

    private void startStarAnimation(final View view, int delay) {
        view.setVisibility(View.INVISIBLE);
        ObjectAnimator anim1 = ObjectAnimator.ofFloat(view, "scaleX", 0.3f, 1.2f);
        ObjectAnimator anim1y = ObjectAnimator.ofFloat(view, "scaleY", 0.3f, 1.2f);
        anim1.setDuration(240);
        anim1y.setDuration(240);
        ObjectAnimator anim2 = ObjectAnimator.ofFloat(view, "scaleX", 1.2f, 0.97f);
        ObjectAnimator anim2y = ObjectAnimator.ofFloat(view, "scaleY", 1.2f, 0.97f);
        anim2.setStartDelay(240);
        anim2.setDuration(480);
        anim2y.setStartDelay(240);
        anim2y.setDuration(480);
        ObjectAnimator anim3 = ObjectAnimator.ofFloat(view, "scaleX", 0.97f, 1.01f);
        anim3.setStartDelay(240 + 480 + 40);
        anim3.setDuration(480);
        ObjectAnimator anim3y = ObjectAnimator.ofFloat(view, "scaleY", 0.97f, 1.01f);
        anim3y.setStartDelay(240 + 480 + 40);
        anim3y.setDuration(480);
        ObjectAnimator anim4 = ObjectAnimator.ofFloat(view, "scaleX", 1.01f, 1f);
        anim4.setStartDelay(240 + 480 + 40 + 480);
        anim4.setDuration(240);
        ObjectAnimator anim4y = ObjectAnimator.ofFloat(view, "scaleY", 1.01f, 1f);
        anim4y.setStartDelay(240 + 480 + 40 + 480);
        anim4y.setDuration(240);

        final AnimatorSet set = new AnimatorSet();
        set.playTogether(anim1, anim2, anim3, anim4, anim1y, anim2y, anim3y, anim4y);
        view.postDelayed(new Runnable() {
            @Override
            public void run() {
                view.setVisibility(View.VISIBLE);
                set.start();
            }
        }, delay);
    }

    @Override
    public void onClick(View v) {
        int num = 0;
        if (v == mStarOne) {
            num = 1;
        } else if (v == mStarTwo) {
            num = 2;
        } else if (v == mStarThree) {
            num = 3;
        } else if (v == mStarFour) {
            num = 4;
        } else if (v == mStarFive) {
            num = 5;
        }
        if (mOnRatingClickListener != null) {
            mOnRatingClickListener.onClick(num);
        }
    }

    public void setOnRatingClickListener(OnRatingClickListener listener) {
        mOnRatingClickListener = listener;
    }


    public interface OnRatingClickListener {
        public void onClick(int num);
    }
}
